package com.lhy.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;

import java.util.ArrayList;
import java.util.List;

@CanalEventListener //声明当前的类是canal的监听类
public class BusinessListener {
    /**
     *
     * @param eventType 当前操作数据库的类型
     * @param rowData 当前操作数据库的数据
     */
    @ListenPoint(schema = "it_lhy_goods",table = "ad")
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        System.out.println("广告表数据发生改变");
        //获取改变之前的数据
//        rowData.getBeforeColumnsList().forEach((c)-> System.out.println("改变前的数据:"+c.getName()+"::"+c.getValue()));

        //获取改变之后的数据
//        rowData.getAfterColumnsList().forEach((c)-> System.out.println("改变之后的数据:"+c.getName()+"::"+c.getValue()));
        List<CanalEntry.Column> listBefore=rowData.getBeforeColumnsList();
        int before=0;
        int after=0;
        for (CanalEntry.Column c : listBefore){
            if (c.getName().equals("status")) {
                before= Integer.parseInt(c.getValue());
            }
        }
        List<CanalEntry.Column> listAfter=rowData.getAfterColumnsList();
        for (CanalEntry.Column c : listAfter){
            if (c.getName().equals("status")) {
                after= Integer.parseInt(c.getValue());
            }
        }
        if (before<after){
            System.out.println("广告已下架");
        }
        if (before>after){
            System.out.println("广告已上架");
        }


    }
}


